<?php

namespace app\api\service;

use app\api\model\AccountType;
use app\api\model\SupplierModel;
use think\Db;
use app\api\model\SupplierReconciliation as Reconciliation;

class SupplierReconciliation
{
    use SetAccountFlow;
    use GetFlowSum;

    /**
     *  供应商对账单收款
     * @param $data
     * @return mixed
     */
    public static function receipt($data)
    {
        return Db::transaction(function () use ($data) {
            $res = [];
            $res[] = Reconciliation::create($data);
            $data['customer_id'] = $data['supplier_id'];
            $data['customer_type'] = 'supplier';
            $res[] = self::newReconciliationFlow($data);
            if (isset($data['discounted_price'])) {
                $res[] = self::newDiscountedPrice($data);
            }
            $res[] = self::updateSupplierArrears($data);
            if (empty(array_filter($res))) {
                return false;
            } else {
                return true;
            }
        });
    }

    /**
     * 期初调整
     * @param $data
     * @return mixed
     */
    public static function adjustment($data)
    {
        return Db::transaction(function () use ($data) {
            $data['customer_id'] = $data['supplier_id'];
            $data['customer_type'] = 'supplier';
            $res[] = self::newAdjustment($data);
            $res[] = self::adjustmentSupplierArrears($data);
            if (empty(array_filter($res))) {
                return false;
            } else {
                return true;
            }
        });
    }

    /**
     * 新增期初调整流水
     * @param $data
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected static function newAdjustment($data)
    {
        $flowData = $data;
        $flowData['account_type_id'] = AccountType::where('name', '期初调整')->find()['id'];
        $flowData['business_type'] = '期初调整收款';
        $flowData['business_number'] = $data['number'];
        $flowData['in_amount'] = $data['amount'];
        return self::newFlow($flowData);
    }

    /**
     * 新增收款对账流水
     * @param $data
     * @return bool
     */
    protected static function newReconciliationFlow($data)
    {
        $flowData = $data;
        $flowData['business_type'] = '供应商对账单收款';
        $flowData['business_number'] = $data['number'];
        $flowData['in_amount'] = $data['amount'];
        return self::newFlow($flowData);
    }

    /**
     * 新增优惠对账流水
     * @param $data
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected static function newDiscountedPrice($data)
    {
        $flowData = $data;
        $flowData['account_type_id'] = AccountType::where('name', '优惠金额')->find()['id'];
        $flowData['business_type'] = '优惠金额收款';
        $flowData['business_number'] = $data['number'];
        $flowData['in_amount'] = $data['discounted_price'];
        return self::newFlow($flowData);
    }

    /**
     * 更新供应商欠款数
     * @param $data
     * @return mixed
     */
    protected static function updateSupplierArrears($data)
    {
        $supplier = SupplierModel::find($data['supplier_id']);
        $arrears = $supplier['arrear'] - $data['amount'];
        if (isset($data['discounted_price'])  && !empty($data['discounted_price'])) {
            $arrears = $arrears - $data['discounted_price'];
        }
        return $supplier->save(['arrear' => $arrears]);
    }

    /**
     * 调整供应商欠款数
     * @param $data
     * @return mixed
     */
    protected static function adjustmentSupplierArrears($data)
    {
        $supplier = SupplierModel::find($data['supplier_id']);
        $beginning = $data['amount'];
        $oldBeginning = $supplier['beginning'];
        $oldAmount = $supplier['arrear'];
        if ($oldBeginning) {
            //  更新时 余额为 原金额-原期初金额+期初金额
            $amount = ($oldAmount - $oldBeginning) + $beginning;
            return $supplier->save(['arrear' => $amount]);
        } else {
            // 新增时 余额为 原金额+期初金额
            $amount = $oldAmount + $beginning;
            return $supplier->save(['arrear' => $amount, 'beginning' => $beginning]);
        }
    }
}
